<?php
	DEFINE('H1', 'Reading RSS Feeds');
	DEFINE('TITLE','Reading RSS Feeds');
	
	$INCL1 = 'style/RSSStyle';
	global $INCL1;
	
	require('includes/header.php');
?>	
	<h2><?PHP echo H1 ?></h2>

<?php
	class Element {
		private $name;
		private $value;
		private $label;
		
		public function __construct () {
		}
		public function setName ($name) {
			$this->name = $name;	
		}
		public function getName () {
			return $this->name;
		}
		public function setValue($value) {
			$this->value = $value;
		}
		public function getValue() {
			return $this->value;
		}
		public function setLabel ($label) {
			$this->label = $label;
		}
		public function getLabel() {
			return $this->label;
		}
	}
?>
<?php
	class Option extends Element {
	
		public function __construct($value='', $label='') {
			parent::__construct();
			$this->setValue($value);
			$this->setLabel($label);
		}
		
		public function render() {
			echo "<option value=\"" . $this->getValue() . "\">" . $this->getLabel() . "</option>\n";
		}
	}
?>
<?php 
	class Select extends Element {
		protected $options;
		public function _construct(){
			parent::_construct();
			$this->options = array();
		}
		public function setOption($option) {
			$this->options[] = $option;
		}
		private function getOption() {
			return (array)$this->options;
		}
		public function render() {
			echo $this->getLabel() . ": <br />\n";
			echo "<select name=\"" . $this->getName() . "\">\n";
			foreach ($this->getOption() as $opt) {
				echo $opt->render();
			}
			echo "</select>";
		}
	}
?>

<?php 
	if (!isset($_POST['submit'])) { 
?>

<form method="post" action="readRSS3.php">

<?php 
	$news = new Select();
	$news->setLabel('RSS Feeds');
	$news->setName('news_sel');
	$news->setOption(new Option('http://www.newsvine.com/_feeds/rss2/tag?id=technology','NewsVine Tech'));
	$news->setOption(new Option('http://online.wsj.com/xml/rss/3_7011.xml','WSJ Home US'));
	$news->setOption(new Option('http://online.wsj.com/xml/rss/3_8068.xml','WSJ US News'));
	$news->setOption(new Option('http://online.wsj.com/xml/rss/3_7087.xml','WSJ Politics &amp; Campaign'));
	$news->setOption(new Option('http://online.wsj.com/xml/rss/3_7435.xml','WSJ Journal Reports'));
	$news->setOption(new Option('http://online.wsj.com/xml/rss/3_7455.xml','WSJ Technology: What\'s News'));
	$news->setOption(new Option('http://online.wsj.com/xml/rss/3_7454.xml','WSJ Personal Technology'));
	$news->render();
?>

<input type="submit" name="submit" value="Go" />
</form>

<?php } else 
	{ 
	$x = $_POST['news_sel'];
	$xml = simplexml_load_file($x) 
		 	or die("ERROR: Cannot read RSS feed");
?>
			
<form method="post" action="readRSS3.php">

<?php 
	$news = new Select();
	$news->setLabel('RSS Feeds');
	$news->setName('news_sel');
	$news->setOption(new Option('http://www.newsvine.com/_feeds/rss2/tag?id=technology','NewsVine Tech'));
	$news->setOption(new Option('http://online.wsj.com/xml/rss/3_7011.xml','WSJ Home US'));
	$news->setOption(new Option('http://online.wsj.com/xml/rss/3_8068.xml','WSJ US News'));
	$news->setOption(new Option('http://online.wsj.com/xml/rss/3_7087.xml','WSJ Politics &amp; Campaign'));
	$news->setOption(new Option('http://online.wsj.com/xml/rss/3_7435.xml','WSJ Journal Reports'));
	$news->setOption(new Option('http://online.wsj.com/xml/rss/3_7455.xml','WSJ Technology: What\'s News'));
	$news->setOption(new Option('http://online.wsj.com/xml/rss/3_7454.xml','WSJ Personal Technology'));
	$news->setOption(new Option('http://www.npr.org/rss/rss.php?id=1008','NPR Topics: Arts & Life'));
	$news->render();
?>

<input type="submit" name="submit" value="Go" />
</form>

<h3 style="heading"><?php echo $xml->channel->title; ?></h3>
	
<?php		
	foreach ($xml->channel->item as $item) {
?>

<ul>
	<li>
		<div class="story">
			<div class="headline">
				<a href="<?php echo $item->link; ?>">
					<?php echo $item->title; ?>
				</a>
			</div>
			<div class="timestamp"><?php echo $item->pubDate; ?></div>
			<div class="body"><?php echo $item->description; ?></div>
		</div>
	</li>
</ul>
<?php
		}
	}
?>

<?php
	require('includes/footer.php');
?>